package com.linko.intelligent.sys.core.controller;

import com.github.pagehelper.PageInfo;
import com.linko.intelligent.base.CommonResponse;
import com.linko.intelligent.sys.core.service.AttachService;
import com.linko.intelligent.sys.pojo.dto.AttachDto;
import com.linko.intelligent.sys.pojo.po.Attach;
import com.linko.intelligent.sys.pojo.query.AttachQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author yuan
 * @create 2024/3/15 11:42
 */
@RestController
@RequestMapping("/attach")
@Api(value = "AttachController", tags = "附件")
public class AttachController {

    @Autowired
    AttachService attachService;

    /**
     * 单附件上传
     *
     * @param file
     * @return
     */
    @ApiOperation("单附件上传")
    @PostMapping("/{s}/{m}")
    public Object save(@RequestParam("file") MultipartFile file
            , @PathVariable("s") String s
            , @PathVariable("m") String m) {
        Attach attach = attachService.save(file, s, m);
        return new CommonResponse<Attach>(1, "成功", attach);
    }

    /**
     * 多附件上传
     *
     * @param files
     * @return
     */
    @ApiOperation("多附件上传")
    @PostMapping("/all/{s}/{m}")
    public Object saveAll(@RequestParam("file") List<MultipartFile> files
            , @PathVariable("s") String s
            , @PathVariable("m") String m) {
        List<Attach> attachs = attachService.saveAll(files, s, m);
        return new CommonResponse<List<Attach>>(1, "成功", attachs);
    }

    /**
     * 删除附件
     */
    @ApiOperation("删除附件")
    @DeleteMapping("/{id}")
    public Object delById(@PathVariable("id") Long id) {
        attachService.removeById(id);
        return new CommonResponse(1, "成功");
    }

    /***
     * 根据ids获取附件列表
     * @param ids
     * @return
     */
    @ApiOperation("根据ids获取附件列表")
    public Object queryListByIds(@PathVariable("ids") String ids) {
        List<Long> idList = Arrays.stream(ids.split(","))
                .filter(it -> StringUtils.isNotBlank(it)).map(it -> Long.parseLong(it))
                .collect(Collectors.toList());
        AttachQuery query = new AttachQuery();
        query.setIds(idList);
        List<Attach> attaches = attachService.queryList(query);
        return new CommonResponse<List<Attach>>(1, "成功", attaches);
    }


    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public CommonResponse<Attach> get(@PathVariable("id") Long id) {
        Attach po = attachService.getById(id);
        return new CommonResponse<Attach>(CommonResponse.SUCCESS, "成功", po);
    }

    @ApiOperation("详情-关联内容")
    @GetMapping(value = "/{id}/detail")
    public CommonResponse<Attach> detail(@PathVariable("id") Long id) {
        Attach po = attachService.getDetail(id);
        return new CommonResponse<Attach>(CommonResponse.SUCCESS, "成功", po);
    }

    @ApiOperation("列表")
    @GetMapping(value = "/list")
    public CommonResponse<List<Attach>> list(@SpringQueryMap AttachQuery query) {
        List<Attach> list = attachService.queryList(query);
        return new CommonResponse<>(CommonResponse.SUCCESS, "查询成功", list);
    }

    /***
     * @param query
     * @return
     */
    @ApiOperation("分页")
    @GetMapping(value = "/page")
    public CommonResponse<PageInfo<Attach>> page(@SpringQueryMap AttachQuery query) {
        PageInfo<Attach> pageInfo = attachService.queryPage(query);
        return new CommonResponse<>(CommonResponse.SUCCESS, "查询成功!", pageInfo);
    }

    @ApiOperation("修改")
    @PutMapping(value = "/{id}/update")
    public CommonResponse update(@PathVariable("id") Long id, @RequestBody AttachDto dto) {
        attachService.update(id, dto);
        return new CommonResponse<>(CommonResponse.SUCCESS, "成功");
    }

}
